package com.zd.wechatPlatform.common.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 字符串工具类
 * 
 * @author lishi
 * @date 2017-11-29 下午8:52:12
 */
public class StringUtils {

	public static String getRandomWithTimestamp(int length) throws Exception {
		if (length < 20 || length > 40) {
			throw new Exception("if the length small than 20 large than 32,not advise use this method!");
		}

		int startIndex = 32 - length + 13; // UUID字符串去掉"-"长度为32。 时间戳占12位长度
		return System.currentTimeMillis() + UUID.randomUUID().toString().replaceAll("-", "").substring(startIndex);
	}

	public static boolean isBank(String str) {
		if (str == null)
			return true;
		if ("".equals(str.replaceAll(" ", "")))
			return true;
		return false;
	}

	private static final char SEPARATOR = '_';

	public static String toUnderlineName(String s) {
		if (s == null) {
			return null;
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			boolean nextUpperCase = true;
			nextUpperCase = Character.isUpperCase(s.charAt(i));
			if (nextUpperCase) {
				if (i > 0)
					sb.append(SEPARATOR);
			}
			sb.append(Character.toLowerCase(c));
		}
		return sb.toString();
	}
	
	public static String stringToUnicode(String string) {
        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);  // 取出每一个字符
            unicode.append("\\u" +Integer.toHexString(c));// 转换为unicode
        }
        return unicode.toString();
    }
	
	
	public static String dictSort(Map<String,Object> paramMap) {
		String src = "";
		List<String> keyList = new ArrayList<>();
		for (String key : paramMap.keySet()) {
			keyList.add(key);
		}
		Collections.sort(keyList);
		for (String key : keyList) {
			src += (key + "=" + paramMap.get(key) + "&");
		}
		src = src.substring(0, src.length() - 1);
		return src;
	}
	
	
	public static void main(String[] args) throws Exception{
		System.out.println(getRandomWithTimestamp(20));
	}
}
